package cn.acewill.yxd.yunxundian.ui.sign;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.acewill.yxd.yunxundian.BaseActivity;
import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.adapter.CollegueAdapter;
import cn.acewill.yxd.yunxundian.adapter.EvaApdater;
import cn.acewill.yxd.yunxundian.componet.BaseFragment;
import cn.acewill.yxd.yunxundian.model.CollegueSchedule;
import cn.acewill.yxd.yunxundian.model.Schedule;
import cn.acewill.yxd.yunxundian.ui.MainActivity;
import cn.acewill.yxd.yunxundian.ui.sign.Contract.MEvaluationView;
import cn.acewill.yxd.yunxundian.ui.sign.Presenter.OEvaLoadPresenterIml;

/**
 * Created by xxx on 2018/1/11.
 */

public class EvaluationForOtherFragment extends BaseFragment implements MEvaluationView{

    private boolean isPrepared = false;
    private boolean mHasLoadedOnce = false;
    private EvaApdater adapter;
    private CollegueAdapter clAdapter;
    private RecyclerView myRecycleView;
    private OEvaLoadPresenterIml presenterIml;
    private String currentYear,currentMonth;
    private RelativeLayout loadingRel;
    private LinearLayout dateLayout;
    private String currentdate;
    public static  boolean isRefresh = false; //选择日期刷新
    public static boolean searchRefresh = false;
   // private SearchView search_bar;
    private LinearLayout nodata;
    private List<Schedule> dataList;
    private int loadFrom = 0;
    private int shopId = 0;


    public static EvaluationForOtherFragment getInstance(int loadfrom,int shopid){
        Bundle bundle = new Bundle();
        bundle.putInt("loadfrom",loadfrom);
        bundle.putInt("shopid",shopid);
        EvaluationForOtherFragment fragment = new EvaluationForOtherFragment();
        fragment.setArguments(bundle);
        return fragment;

    }
    public void initData(){
        Bundle bundle = getArguments();
        if (bundle != null) {
            loadFrom = bundle.getInt("loadfrom",1);
            shopId = bundle.getInt("shopid",0);
        }
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        initData();
        View view = LayoutInflater.from(getContext()).inflate(R.layout.base_evaluation_view,container,false);
        myRecycleView = (RecyclerView) view.findViewById(R.id.myRecycle);
        myRecycleView.setLayoutManager(new LinearLayoutManager(getContext()));
        myRecycleView.setNestedScrollingEnabled(false);
        loadingRel = (RelativeLayout) view.findViewById(R.id.loadingRel);
        dateLayout = (LinearLayout) view.findViewById(R.id.dateLayout);
        nodata = view.findViewById(R.id.nodata);
        isPrepared = true;
        presenterIml = new OEvaLoadPresenterIml(this);

        lazyLoad();
        return view;
    }

    @Override
    protected void lazyLoad() {
        if (isRefresh){
            if (presenterIml!=null){
                String[] res = MyEvaluationActivity.currentData.split("-");
                currentYear = res[0];
                currentMonth = res[1];
                if (loadFrom == 0){
                    //加载门店数据
                    presenterIml.requestShopData(MainActivity.id,currentYear,currentMonth);
                }else{
                    presenterIml.requestData(MainActivity.id,currentYear,currentMonth);
                }
            }
            return;
        }
        if (searchRefresh){
            if (MyEvaluationActivity.searchText!=null){

                if (adapter!=null){
                    if (loadFrom!=0)
                        adapter.getFilter().filter(MyEvaluationActivity.searchText);
                }
                if (clAdapter!=null){
                    if (loadFrom==0)
                        clAdapter.setFilter().filter(MyEvaluationActivity.searchText);
                }
            }

        }
        if (!isPrepared||!isVisible||mHasLoadedOnce){
            return;
        }
        if (presenterIml!=null){
            String[] res = MyEvaluationActivity.currentData.split("-");
            currentYear = res[0];
            currentMonth = res[1];
            if (loadFrom == 0){
                presenterIml.requestShopData(MainActivity.id,currentYear,currentMonth);
            }else{
                presenterIml.requestData(MainActivity.id,currentYear,currentMonth);
            }
            mHasLoadedOnce = true;
        }

    }

    @Override
    public void showLoadingView() {
        loadingRel.setVisibility(View.VISIBLE);
        dateLayout.setVisibility(View.GONE);
        nodata.setVisibility(View.GONE);
    }

    @Override
    public void hideLoadingView() {
        loadingRel.setVisibility(View.GONE);
        dateLayout.setVisibility(View.VISIBLE);
        nodata.setVisibility(View.GONE);
        if (isRefresh){
            isRefresh = false;
        }
        if (searchRefresh){
            searchRefresh = false;
        }
    }

    @Override
    public void updateAdapter(List<Schedule> rrList) {

        dataList = rrList;
        setAdapter(dataList);
        if (adapter!=null){
            adapter.getFilter().filter(MyEvaluationActivity.searchText);
        }

    }

    //其他人的日程
    @Override
    public void loadCollegSch(List<CollegueSchedule> csList) {
        setClAdapter(csList);
        if (clAdapter!=null){
            clAdapter.setFilter().filter(MyEvaluationActivity.searchText);
        }
    }

    @Override
    public void showNoData() {
        nodata.setVisibility(View.VISIBLE);
        dateLayout.setVisibility(View.GONE);
        loadingRel.setVisibility(View.GONE);
    }

    @Override
    public void refreshAdapter() {

    }

    @Override
    public void requestErro() {
        Toast.makeText(getContext(),"数据请求失败",Toast.LENGTH_SHORT).show();
    }

    public void setAdapter(List<Schedule> rrList){
        adapter = new EvaApdater(getContext(), rrList, new EvaApdater.OnBtnClickListener() {
            @Override
            public void dotaskListener(Schedule schedule) {

            }

            @Override
            public void signListener(Schedule schedule) {

            }

            @Override
            public void submitListener(Schedule schedule) {

            }

            @Override
            public void shareListener(Schedule schedule) {

            }

            @Override
            public void lookListener(Schedule schedule) {
                //查看
                Intent in=new Intent(getContext(),LookEvaDetialActivity.class)
                        .putExtra("schid",schedule.getID());
                startActivity(in);
            }

            @Override
            public void sendListener(Schedule schedule) {
                startActivity(new Intent(getContext(),SendMailActivity.class).putExtra("schId",schedule.getID()));

            }

        },1);
        myRecycleView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

    public void setClAdapter(List<CollegueSchedule>clList){
        clAdapter = new CollegueAdapter(getContext(), clList, new CollegueAdapter.OnBtnClickListener() {
            @Override
            public void submitListener(CollegueSchedule cs) {

            }

            @Override
            public void shareListener(CollegueSchedule cs) {

            }

            @Override
            public void lookListener(CollegueSchedule cs) {
                //查看
                Intent in=new Intent(getContext(),LookEvaDetialActivity.class)
                        .putExtra("schid",cs.getSchid());
                startActivity(in);
            }

            @Override
            public void sendMailListener(CollegueSchedule cs) {
                startActivity(new Intent(getContext(),SendMailActivity.class).putExtra("schId",cs.getSchid()).putExtra("otherSend",1));
            }
        });
        myRecycleView.setAdapter(clAdapter);
        clAdapter.notifyDataSetChanged();
    }

}
